export default {
    data() {
        return {
            loopList: [],
            isAddShow: false,
            formList: {
                loop_id: 0,
                loop_pic: ''
            }
        }
    },
    created() {
        this.getAllLoop()
    },
    methods: {
        clearForm() {
            this.formList.loop_pic = ''
        },
        submitForm() {
            let loops = this.formList
            this.axios.defaults.headers.common['Authorization'] = localStorage['token']
            this.axios.post('api/addLoops',
                { loop: loops })
                .then(res => {
                    this.$message({
                        type: 'success',
                        duration: 1000,
                        message: '轮播图添加成功'
                    });
                    this.getAllLoop()
                    this.isAddShow = false
                    this.clearForm()
                })
                .catch(err => {
                    this.$message({
                        type: 'error',
                        duration: 1000,
                        message: '轮播图添加失败'
                    });
                })
        },
        handleClose(done) {
            done();
        },
        addLoop() {
            this.isAddShow = true
        },
        getAllLoop() {
            this.axios.get("api/getLoopsInfo").then(res => {
                this.loopList = res.data.data;
                this.loopList.sort((a, b) => {
                    return a.loop_id - b.loop_id;
                });
                this.formList.loop_id = this.loopList.length + 1
            });
        },
        indexMethod(index) {
            return index + 1;
        },
        upLoop(e) {
            let tempId = 999
            let tempPic = '123'
            if (e == 1) {
                this.$message({
                    type: 'error',
                    duration: 1000,
                    message: '不能再上调了'
                });
                return
            }
            tempId = this.loopList[e - 1].loop_id
            tempPic = this.loopList[e - 1].loop_pic

            this.loopList[e - 1].loop_id = e - 1
            this.loopList[e - 1].pic = this.loopList[e - 2].pic

            this.loopList[e - 2].loop_id = tempId
            this.loopList[e - 2].pic = tempPic
            this.loopList.sort((a, b) => {
                return a.loop_id - b.loop_id;
            });
        },
        downLoop(e) {
            let tempId = 999
            let tempPic = '123'
            if (e == this.loopList.length) {
                this.$message({
                    type: 'error',
                    duration: 1000,
                    message: '不能再下调了'
                });
                return
            }

            tempId = parseInt(this.loopList[e - 1].loop_id) + 1
            tempPic = this.loopList[e - 1].loop_pic

            this.loopList[e].loop_id = e
            this.loopList[e].pic = this.loopList[e - 1].pic

            this.loopList[e - 1].loop_id = tempId
            this.loopList[e - 1].pic = tempPic
            this.loopList.sort((a, b) => {
                return a.loop_id - b.loop_id;
            });
        },
        handleDelete(e) {
            console.log('handleDelete', e);
            this.$confirm('删除后不可恢复, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.axios.defaults.headers.common['Authorization'] = localStorage['token']
                this.axios.delete('api/delLoops', { params: { id: e } })
                    .then(res => {
                        this.getAllLoop()
                        this.$message({
                            type: 'success',
                            duration: 1000,
                            message: '删除成功'
                        });
                    })
            }).catch(() => {
            });
        }
    }
}